﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Drawing.Design;
using System.Windows.Forms;

namespace Axe.ComponentModel
{
    public class DropDownUserControl : UserControl, IObjectEditor
    {
        public virtual void SetEditedObject(object value, object parentProperty)
        {
            throw new NotImplementedException();
        }

        public virtual object GetEditedObject()
        {
            throw new NotImplementedException();
        }
    }

    public abstract class DropDownEditor : UITypeEditor
    {
        protected abstract DropDownUserControl GetEditingControl();

        /// <summary>
        /// Реализация метода редактирования
        /// </summary>
        public override Object EditValue(ITypeDescriptorContext context,IServiceProvider provider,Object value)
        {
            if ((context != null) && (provider != null))
            {
                IWindowsFormsEditorService svc =
                  (IWindowsFormsEditorService)
                  provider.GetService(typeof(IWindowsFormsEditorService));

                if (svc != null)
                {
                    DropDownUserControl flctrl = GetEditingControl();
                    flctrl.SetEditedObject(value,context.Instance);
                    flctrl.Tag = svc;

                    svc.DropDownControl(flctrl);

                    value = flctrl.GetEditedObject();
                }
            }

            return base.EditValue(context, provider, value);
        }

        /// <summary>
        /// Возвращаем стиль редактора - выпадающее окно
        /// </summary>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null)
                return UITypeEditorEditStyle.DropDown;
            else
                return base.GetEditStyle(context);
        }

    }
}
